package com.menker.app.ui.generalweb;


import android.app.Activity;
import android.text.TextUtils;

import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.menker.app.MyApp;
import com.menker.app.R;
import com.menker.app.biz.PageListInfo;
import com.menker.app.common.Utils;
import com.menker.app.di.DaggerScope;
import com.menker.app.di.Injector;
import com.menker.app.rest.ApiHelper;
import com.menker.app.rest.result.ApiResult;
import com.menker.app.rest.result.ShareInfo;
import com.menker.app.rest.result.UploadResult;
import com.menker.app.util.ShareUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import cn.sharesdk.framework.Platform;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;

/**
 * @author J.Huang
 *
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(GeneralWebViewPresenter.class)
class GeneralWebViewPresenter extends MvpBasePresenter<GeneralWebViewView> {

    @Inject
    ApiHelper mApiHelper;

    private String mStrErrorApi;

    private Activity mActivity;

    private Subscriber<ShareInfo> mGetShareInfoSubscriber = null;
    private GeneralWebViewActivity.OnUploadImgListener mOnUploadImgListener;
    private Subscriber<ShareInfo> mGetShareSubscriber = null;
    private GeneralWebViewActivity.OnSharePicListener mOnSharePicListener;

    GeneralWebViewPresenter(Activity context) {
        mActivity = context;

        mStrErrorApi = mActivity.getResources().getString(R.string.error_api);

        Injector.initGeneralWebViewPresenterComponent().inject(this);
    }

    @Override
    public void attachView(GeneralWebViewView view) {
        super.attachView(view);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mGetShareInfoSubscriber != null) {
            mGetShareInfoSubscriber.unsubscribe();
            mGetShareInfoSubscriber = null;
        }

        if (mGetShareSubscriber != null) {
            mGetShareSubscriber.unsubscribe();
            mGetShareSubscriber = null;
        }

        if (mOnUploadImgListener != null) {
            mOnUploadImgListener = null;
        }

        super.detachView(retainInstance);
    }

    void getShareInfo(String itemId) {
        mGetShareInfoSubscriber = new GetShareInfoSubscriber();
        mApiHelper.getShareInfo(mGetShareInfoSubscriber, itemId);
    }

    void getShare(PageListInfo pageList) {

        mGetShareSubscriber = new GetShareSubscriber();
        mApiHelper.getShare(mGetShareSubscriber, pageList);

    }

    void uploadImg(String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            return;
        }

        RequestBody requestBody =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        Map<String, RequestBody> maps = new HashMap<>();
        maps.put("file\"; filename=\"avatar.jpg\"",requestBody);

        mApiHelper.uploadImg(new UploadImgSubscriber(),maps,fileName);
    }

    void setOnUploadImgListener(GeneralWebViewActivity.OnUploadImgListener onUploadImgListener) {
        mOnUploadImgListener = onUploadImgListener;
    }

    void setOnSharePicListener(GeneralWebViewActivity.OnSharePicListener onSharePicListener) {
        mOnSharePicListener = onSharePicListener;
    }

    public void sharePic(String url) {
        if (mOnSharePicListener != null) {
            ShareUtils.sharePic(mActivity, url, mOnSharePicListener);
        }
    }

    private class UploadImgSubscriber extends Subscriber<UploadResult> {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(UploadResult uploadResult) {
            if (uploadResult.Total > 0) {
                mOnUploadImgListener.onFinish(uploadResult.Data.get(0));
            }
        }
    }

    private class GetShareInfoSubscriber extends Subscriber<ShareInfo> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastLong(mStrErrorApi);
        }

        @Override
        public void onNext(ShareInfo shareInfo) {
            if (shareInfo.checkState(mActivity)) {
                if (getView() != null) {
                    getView().updateToolbar(shareInfo);
                }
            } else {
            }
        }
    }

    private class GetShareSubscriber extends Subscriber<ShareInfo> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastLong(mStrErrorApi);
        }

        @Override
        public void onNext(ShareInfo shareInfo) {
            if (shareInfo.checkState(mActivity)) {
                if (getView() != null) {
                    getView().updateToolbar(shareInfo);
                }
            }
        }
    }
}
